<?php
class NewsAction extends Action{

    public $news="news_id,news_title,news_image_s,news_url,news_author,news_intro,news_time,news_mark,news_sort,news_readNum,news_category,news_channel";
    public $news_comments="comment_id,uid,content,time_insert";


    public function get_newslist(){

        if(!Database($_REQUEST["project"])) exit;

        $_REQUEST['news_category']  =$_REQUEST['news_category']==''?1:$_REQUEST['news_category'];
        $_REQUEST['news_channel']   =$_REQUEST['news_channel']==''?1:$_REQUEST['news_channel'];
        $sql = "select ".$this->news." from news where news_display=1 and news_category=".$_REQUEST['news_category'];
	
	    $_REQUEST['count']  == null && $_REQUEST['count'] = 10;
        $_REQUEST['page']   == null && $_REQUEST['page']  = 1;
	    $_REQUEST['mark']   != null && $sql.=" and news_mark=".$_REQUEST['mark'];
        $_REQUEST['startid']!= null && $sql.=" and news_id>=".$_REQUEST['startid'];
        $_REQUEST['endid']  != null && $sql.=" and news_id<=".$_REQUEST['endid'];
        $sql.=" and news_channel =".$_REQUEST['news_channel'];
        $sql.=" order by news_time desc";
        $sql .= " limit ".(($_REQUEST['page']-1)*$_REQUEST['count']).",".$_REQUEST['count'];
        $rs=M("news","",'DB_MEETING')->query($sql);
        //echo
	    if($rs){
            foreach($rs as $val){
                if($val['news_image_s']!='')
                    $val['news_image_s']=C('DOMAIN_NAME').__ROOT__.'/'.$val['news_image_s'];
                if($val['news_url']!='')
                    $val['news_url']=C('DOMAIN_NAME').__ROOT__.'/'.$val['news_url'];
                $val['news_commentNum']=M('news_comment','','DB_MEETING')->where('news_id='.$val['news_id'])->count();
                $val['news_votesNum']=M('news_votes','','DB_MEETING')->where('news_id='.$val['news_id'])->count();
                $result[]=$val;
            }
            return_json('0',$result);
        }else{
            return_json('40001');
        }
    }

    public function is_read(){
        if(!Database($_POST['pid'])){ exit();}
        $_POST['herf']=str_replace(C('DOMAIN_NAME').__ROOT__.'/','',$_POST['herf']);
        $rs=M('news','','DB_MEETING')->where("news_url='{$_POST['herf']}'")->setInc('news_readNum',1);
        if($rs)
            echo 1;
        else
            echo 0;
    }
    /*
     *add_news_comment
     *记录当前news下载情况接口
     *参数
     *project uid content
     *返回码
     *-10(project缺省)
     *40002(uid缺省) 40005(content缺省) 40031(news_id缺省)
     *0(成功) -1(服务器存储异常)
     ***********************/
    public function add_news_comment(){
        if(!Database($_REQUEST["project"]))return;
        switch(''){
            case $_REQUEST['uid']:is_not_in(40002);return;
            case $_REQUEST['content']:is_not_in(40005);return;
            case $_REQUEST['news_id']:is_not_in(40031);return;
        }
        $_REQUEST['time_insert']=time();
        $rs=M('news_comment','','DB_MEETING')->add($_REQUEST);
        if($rs)
            is_not_in(0);
        else
            is_not_in(-1);
    }
    /*
     *get_news_comment
     *获取新闻评论信息
     *参数
     *project count page startid endid
     *返回码
     *-10(project缺省)
     *40001(当前已无数据) 40031(news_id资料类别缺省)
     *0(成功)
     ***********************/
    public function get_news_comment(){
        if(!Database($_REQUEST["project"]))return;
        switch(''){
            case $_REQUEST['news_id']:is_not_in(40031);return;
        }
        $sql = "select ".$this->news_comments." from news_comment where news_id={$_REQUEST['news_id']}";
        if($_REQUEST['count']==null)	$_REQUEST['count']=10;
        if($_REQUEST['page']==null) 	$_REQUEST['page']=1;
        if($_REQUEST['startid']!=null) 	$sql.=" and suggest_id>=".$_REQUEST['startid'];
        if($_REQUEST['endid']!=null)	$sql.=" and suggest_id<=".$_REQUEST['endid'];
        $sql.=" order by time_insert desc";
        $sql .= " limit ".(($_REQUEST['page']-1)*$_REQUEST['count']).",".$_REQUEST['count'];
	    $rs=M("news_comment","",'DB_MEETING')->query($sql);
        /*foreach($rs as $val){
            $val['votes']=M("information_suggest_votes","",'DB_MEETING')->where('suggest_id='.$val['suggest_id'])->count();
            $rs1[]=$val;
        }*/
        if($rs){
            echo_result('0',$rs);
        }else{
            is_not_in('40001');
        }
    }
    /*
     *add_news_vote
     *点赞接口
     *参数
     *project news_id
     *返回码
     *-10(project缺省)
     *40002(uid缺省) 40031(news_id缺省) 40030(您已经赞过了)
     *0(成功) -1(失败)
     ***********************/
    public function add_news_vote(){
        if(!Database($_REQUEST["project"]))return;
        switch(''){
            case $_REQUEST['uid']:is_not_in(40002);return;
            case $_REQUEST['news_id']:is_not_in(40031);return;
        }
        $_REQUEST['time_insert']=time();
        if(M("news_votes","",'DB_MEETING')->where('uid='.$_REQUEST['uid'].' and news_id='.$_REQUEST['news_id'])->find())
        {is_not_in(40030);return;}
        $rs=M("news_votes","",'DB_MEETING')->add($_REQUEST);
        $num=M("news_votes","",'DB_MEETING')->where('news_id='.$_REQUEST['news_id'])->count();
        if($rs){
            echo_result('0',$num);
        }else{
            is_not_in('-1');
        }
    }

    public function get_sqlite(){
        if(!Database($_REQUEST["cid"]))return;
        try {
            $news_sqlite_path = 'Public/Uploads/sqlite/';
            if (!is_dir($news_sqlite_path)) mkdir($news_sqlite_path, 0777, true);
            $news_sqlite_path .= 'news/';
            if (!is_dir($news_sqlite_path)) mkdir($news_sqlite_path, 0777, true);
            $news_sqlite_path .= $_REQUEST['cid'] . '/';
            if (!is_dir($news_sqlite_path)) mkdir($news_sqlite_path, 0777, true);
//          exit();
            $news_sqlite_path .= 'conference.db';
            exec("rm -rf {$news_sqlite_path}");
            $dbh = new PDO("sqlite:{$news_sqlite_path}");
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//          新闻频道 news_channel表
            $news_channels = "`channel_id`, `channel_name`, `channel_sort`";
            $news_channel = '`channel_id`';
            $sql = "CREATE TABLE news_channel(channel_id INTEGER PRIMARY KEY,channel_name Varchar DEFAULT NULL,channel_sort INTEGER DEFAULT 0,channel_state INTEGER DEFAULT 0);";
            $sql .= $this->get_database_for_sqlite('news_channel', $news_channels, $news_channel, 1);
//          news 单数据同步
            $sql .= "CREATE TABLE news(news_id integer PRIMARY KEY,news_title Varchar DEFAULT NULL,news_image_s Varchar DEFAULT NULL,news_url Varchar DEFAULT NULL,news_author Varchar DEFAULT NULL,news_intro Varchar DEFAULT NULL,news_time integer,news_mark integer,news_sort integer,news_readNum integer,news_category integer,news_commentNum integer,news_votesNum integer,news_channel integer);";

            if($dbh->exec($sql)){
                $rs =array(
                    'sqlite_path'       => C('DOMAIN_NAME').__ROOT__.'/'.$news_sqlite_path,
                    'sqlite_version'    =>1.0,
                    'sqlite_must_update'=>0
                );
                return_json(0,$rs);
            }
        } catch (Exception $e) {
            echo "error!!:$e";
            exit;
        }
    }

    //sqlite
    function get_database_for_sqlite($table,$fields,$field,$last){
        $sql['insert']='select '.$fields.' from `'.$table.'` where time_insert>='.$last.' and time_delete < time_insert';
        $add_list 	= M("","","DB_MEETING")->query($sql['insert']);
        if($add_list==null || count($add_list)==0)
            $sql='';
        else{
            $sql='';
            foreach($add_list as $val){
                switch($table){
                    case 'news_channel':
                        $sql.="insert into news_channel(channel_id,channel_name,channel_sort,channel_state)";
                        $sql.=" values('{$val['channel_id']}','{$val['channel_name']}','{$val['channel_sort']}',0);";
                        break;
                }
            }
        }
        return $sql;
    }
}
?>

